{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "name": "Calculating_intersection_over_union.ipynb",
      "provenance": [],
      "include_colab_link": true
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    }
  },
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "view-in-github",
        "colab_type": "text"
      },
      "source": [
        "<a href=\"https://colab.research.google.com/github/PacktPublishing/Modern-Computer-Vision-with-PyTorch/blob/master/Chapter07/Calculating_intersection_over_union.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "VaLHNIS5vFr8"
      },
      "source": [
        "def get_iou(boxA, boxB, epsilon=1e-5):\n",
        "    x1 = max(boxA[0], boxB[0])\n",
        "    y1 = max(boxA[1], boxB[1])\n",
        "    x2 = min(boxA[2], boxB[2])\n",
        "    y2 = min(boxA[3], boxB[3])\n",
        "    width = (x2 - x1)\n",
        "    height = (y2 - y1)\n",
        "    if (width<0) or (height <0):\n",
        "        return 0.0\n",
        "    area_overlap = width * height\n",
        "    area_a = (boxA[2] - boxA[0]) * (boxA[3] - boxA[1])\n",
        "    area_b = (boxB[2] - boxB[0]) * (boxB[3] - boxB[1])\n",
        "    area_combined = area_a + area_b - area_overlap\n",
        "    iou = area_overlap / (area_combined+epsilon)\n",
        "    return iou"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "3bvslWV_viWJ"
      },
      "source": [
        ""
      ],
      "execution_count": null,
      "outputs": []
    }
  ]
}